#include "sm.h"
#include <QDebug>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QtWidgets>

sm::sm(QWidget *parent) : QWidget(parent)
{
    // 初始化所有UI组件指针为nullptr
    m_topMenu = nullptr;
    m_userManageBtn = nullptr;
    m_roleManageBtn = nullptr;
    m_permissionManageBtn = nullptr;
    m_dataBackupBtn = nullptr;
    m_dataRestoreBtn = nullptr;
    m_systemConfigBtn = nullptr;
    m_logViewBtn = nullptr;
    m_aboutBtn = nullptr;
    m_shiftChangeBtn = nullptr;
    m_shiftRecordBtn = nullptr;
    m_inventoryCheckBtn = nullptr;
    m_inventoryRecordBtn = nullptr;
    m_helpBtn = nullptr;
    m_exitBtn = nullptr;
    m_stackedWidget = nullptr;

    initUI();
}

sm::~sm()
{
    // Qt对象树自动管理内存
}

void sm::initUI()
{
    // 主布局（垂直布局）
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    // 创建顶部菜单
    createTopMenu();
    if (m_topMenu) {
        mainLayout->addWidget(m_topMenu);
    }

    // 创建堆叠窗口
    m_stackedWidget = new QStackedWidget(this);
    if (m_stackedWidget) {
        mainLayout->addWidget(m_stackedWidget, 1);

        // 页面1：用户管理
        QWidget *page1 = new QWidget();
        page1->setStyleSheet("background-color: #FFCCCC;");
        QLabel *label1 = new QLabel("用户管理页面", page1);
        label1->setAlignment(Qt::AlignCenter);
        label1->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout1 = new QVBoxLayout(page1);
        layout1->addWidget(label1);

        // 页面2：角色管理
        QWidget *page2 = new QWidget();
        page2->setStyleSheet("background-color: #CCFFCC;");
        QLabel *label2 = new QLabel("角色管理页面", page2);
        label2->setAlignment(Qt::AlignCenter);
        label2->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout2 = new QVBoxLayout(page2);
        layout2->addWidget(label2);

        // 页面3：权限管理
        QWidget *page3 = new QWidget();
        page3->setStyleSheet("background-color: #CCCCFF;");
        QLabel *label3 = new QLabel("权限管理页面", page3);
        label3->setAlignment(Qt::AlignCenter);
        label3->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout3 = new QVBoxLayout(page3);
        layout3->addWidget(label3);

        // 页面4：数据备份
        QWidget *page4 = new QWidget();
        page4->setStyleSheet("background-color: #FFFFCC;");
        QLabel *label4 = new QLabel("数据备份页面", page4);
        label4->setAlignment(Qt::AlignCenter);
        label4->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout4 = new QVBoxLayout(page4);
        layout4->addWidget(label4);

        // 页面5：数据恢复
        QWidget *page5 = new QWidget();
        page5->setStyleSheet("background-color: #FFCCFF;");
        QLabel *label5 = new QLabel("数据恢复页面", page5);
        label5->setAlignment(Qt::AlignCenter);
        label5->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout5 = new QVBoxLayout(page5);
        layout5->addWidget(label5);

        // 页面6：系统设置
        QWidget *page6 = new QWidget();
        page6->setStyleSheet("background-color: #CCFFFF;");
        QLabel *label6 = new QLabel("系统设置页面", page6);
        label6->setAlignment(Qt::AlignCenter);
        label6->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout6 = new QVBoxLayout(page6);
        layout6->addWidget(label6);

        // 页面7：日志查看
        QWidget *page7 = new QWidget();
        page7->setStyleSheet("background-color: #FFD700;");
        QLabel *label7 = new QLabel("日志查看页面", page7);
        label7->setAlignment(Qt::AlignCenter);
        label7->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout7 = new QVBoxLayout(page7);
        layout7->addWidget(label7);

        // 页面8：关于
        QWidget *page8 = new QWidget();
        page8->setStyleSheet("background-color: #DDA0DD;");
        QLabel *label8 = new QLabel("关于页面", page8);
        label8->setAlignment(Qt::AlignCenter);
        label8->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout8 = new QVBoxLayout(page8);
        layout8->addWidget(label8);

        // 页面9：交班操作
        QWidget *page9 = new QWidget();
        page9->setStyleSheet("background-color: #87CEFA;");
        QLabel *label9 = new QLabel("交班操作页面", page9);
        label9->setAlignment(Qt::AlignCenter);
        label9->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout9 = new QVBoxLayout(page9);
        layout9->addWidget(label9);

        // 页面10：交班记录
        QWidget *page10 = new QWidget();
        page10->setStyleSheet("background-color: #98FB98;");
        QLabel *label10 = new QLabel("交班记录页面", page10);
        label10->setAlignment(Qt::AlignCenter);
        label10->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout10 = new QVBoxLayout(page10);
        layout10->addWidget(label10);

        // 页面11：盘点操作
        QWidget *page11 = new QWidget();
        page11->setStyleSheet("background-color: #FFA07A;");
        QLabel *label11 = new QLabel("盘点操作页面", page11);
        label11->setAlignment(Qt::AlignCenter);
        label11->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout11 = new QVBoxLayout(page11);
        layout11->addWidget(label11);

        // 页面12：盘点记录
        QWidget *page12 = new QWidget();
        page12->setStyleSheet("background-color: #ADD8E6;");
        QLabel *label12 = new QLabel("盘点记录页面", page12);
        label12->setAlignment(Qt::AlignCenter);
        label12->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout12 = new QVBoxLayout(page12);
        layout12->addWidget(label12);

        // 页面13：帮助
        QWidget *page13 = new QWidget();
        page13->setStyleSheet("background-color: #F0E68C;");
        QLabel *label13 = new QLabel("帮助页面", page13);
        label13->setAlignment(Qt::AlignCenter);
        label13->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout13 = new QVBoxLayout(page13);
        layout13->addWidget(label13);

        // 页面14：退出
        QWidget *page14 = new QWidget();
        page14->setStyleSheet("background-color: #E6E6FA;");
        QLabel *label14 = new QLabel("退出页面", page14);
        label14->setAlignment(Qt::AlignCenter);
        label14->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout14 = new QVBoxLayout(page14);
        layout14->addWidget(label14);

        // 添加页面到堆叠窗口
        m_stackedWidget->addWidget(page1);   // 0: 用户管理
        m_stackedWidget->addWidget(page2);   // 1: 角色管理
        m_stackedWidget->addWidget(page3);   // 2: 权限管理
        m_stackedWidget->addWidget(page4);   // 3: 数据备份
        m_stackedWidget->addWidget(page5);   // 4: 数据恢复
        m_stackedWidget->addWidget(page6);   // 5: 系统设置
        m_stackedWidget->addWidget(page7);   // 6: 日志查看
        m_stackedWidget->addWidget(page8);   // 7: 关于
        m_stackedWidget->addWidget(page9);   // 8: 交班操作
        m_stackedWidget->addWidget(page10);  // 9: 交班记录
        m_stackedWidget->addWidget(page11);  // 10: 盘点操作
        m_stackedWidget->addWidget(page12);  // 11: 盘点记录
        m_stackedWidget->addWidget(page13);  // 12: 帮助
        m_stackedWidget->addWidget(page14);  // 13: 退出

        // 默认显示第一个页面
        m_stackedWidget->setCurrentIndex(0);
    }
}

void sm::createTopMenu()
{
    // 顶部菜单容器
    m_topMenu = new QWidget(this);
    if (!m_topMenu) return;

    m_topMenu->setFixedHeight(110);
    m_topMenu->setStyleSheet("background-color: #E6F3FF; border-bottom: 1px solid #CCCCCC;");

    // 主菜单布局
    QHBoxLayout *mainMenuLayout = new QHBoxLayout(m_topMenu);
    mainMenuLayout->setContentsMargins(10, 5, 10, 5);
    mainMenuLayout->setSpacing(15);

    // 功能区1：系统管理
    QWidget *area1 = new QWidget(m_topMenu);
    area1->setFixedHeight(100);
    area1->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout1 = new QVBoxLayout(area1);
    layout1->setContentsMargins(5, 5, 5, 5);
    layout1->setSpacing(5);

    QHBoxLayout *btnLayout1 = new QHBoxLayout();
    btnLayout1->setContentsMargins(0, 0, 0, 0);
    btnLayout1->setSpacing(10);

    m_userManageBtn = new QPushButton("用户管理", area1);
    m_userManageBtn->setFixedSize(60, 60);
    m_roleManageBtn = new QPushButton("角色管理", area1);
    m_roleManageBtn->setFixedSize(60, 60);
    m_permissionManageBtn = new QPushButton("权限管理", area1);
    m_permissionManageBtn->setFixedSize(60, 60);
    m_dataBackupBtn = new QPushButton("数据备份", area1);
    m_dataBackupBtn->setFixedSize(60, 60);
    m_dataRestoreBtn = new QPushButton("数据恢复", area1);
    m_dataRestoreBtn->setFixedSize(60, 60);

    btnLayout1->addWidget(m_userManageBtn);
    btnLayout1->addWidget(m_roleManageBtn);
    btnLayout1->addWidget(m_permissionManageBtn);
    btnLayout1->addWidget(m_dataBackupBtn);
    btnLayout1->addWidget(m_dataRestoreBtn);

    QLabel *label1 = new QLabel("系统管理", area1);
    label1->setFixedHeight(20);
    label1->setAlignment(Qt::AlignCenter);
    label1->setStyleSheet("background-color: #FFD700; color: #333333; font-weight: bold; border-radius: 2px;");

    layout1->addLayout(btnLayout1);
    layout1->addWidget(label1);
    mainMenuLayout->addWidget(area1);

    // 功能区2：系统
    QWidget *area2 = new QWidget(m_topMenu);
    area2->setFixedHeight(100);
    area2->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout2 = new QVBoxLayout(area2);
    layout2->setContentsMargins(5, 5, 5, 5);
    layout2->setSpacing(5);

    QHBoxLayout *btnLayout2 = new QHBoxLayout();
    btnLayout2->setContentsMargins(0, 0, 0, 0);
    btnLayout2->setSpacing(10);

    m_systemConfigBtn = new QPushButton("系统设置", area2);
    m_systemConfigBtn->setFixedSize(60, 60);
    m_logViewBtn = new QPushButton("日志查看", area2);
    m_logViewBtn->setFixedSize(60, 60);
    m_aboutBtn = new QPushButton("关于", area2);
    m_aboutBtn->setFixedSize(60, 60);

    btnLayout2->addWidget(m_systemConfigBtn);
    btnLayout2->addWidget(m_logViewBtn);
    btnLayout2->addWidget(m_aboutBtn);

    QLabel *label2 = new QLabel("系统", area2);
    label2->setFixedHeight(20);
    label2->setAlignment(Qt::AlignCenter);
    label2->setStyleSheet("background-color: #FFA500; color: #333333; font-weight: bold; border-radius: 2px;");

    layout2->addLayout(btnLayout2);
    layout2->addWidget(label2);
    mainMenuLayout->addWidget(area2);

    // 功能区3：交班
    QWidget *area3 = new QWidget(m_topMenu);
    area3->setFixedHeight(100);
    area3->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout3 = new QVBoxLayout(area3);
    layout3->setContentsMargins(5, 5, 5, 5);
    layout3->setSpacing(5);

    QHBoxLayout *btnLayout3 = new QHBoxLayout();
    btnLayout3->setContentsMargins(0, 0, 0, 0);
    btnLayout3->setSpacing(10);

    m_shiftChangeBtn = new QPushButton("交班操作", area3);
    m_shiftChangeBtn->setFixedSize(60, 60);
    m_shiftRecordBtn = new QPushButton("交班记录", area3);
    m_shiftRecordBtn->setFixedSize(60, 60);

    btnLayout3->addWidget(m_shiftChangeBtn);
    btnLayout3->addWidget(m_shiftRecordBtn);

    QLabel *label3 = new QLabel("交班", area3);
    label3->setFixedHeight(20);
    label3->setAlignment(Qt::AlignCenter);
    label3->setStyleSheet("background-color: #90EE90; color: #333333; font-weight: bold; border-radius: 2px;");

    layout3->addLayout(btnLayout3);
    layout3->addWidget(label3);
    mainMenuLayout->addWidget(area3);

    // 功能区4：盘点
    QWidget *area4 = new QWidget(m_topMenu);
    area4->setFixedHeight(100);
    area4->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout4 = new QVBoxLayout(area4);
    layout4->setContentsMargins(5, 5, 5, 5);
    layout4->setSpacing(5);

    QHBoxLayout *btnLayout4 = new QHBoxLayout();
    btnLayout4->setContentsMargins(0, 0, 0, 0);
    btnLayout4->setSpacing(10);

    m_inventoryCheckBtn = new QPushButton("盘点操作", area4);
    m_inventoryCheckBtn->setFixedSize(60, 60);
    m_inventoryRecordBtn = new QPushButton("盘点记录", area4);
    m_inventoryRecordBtn->setFixedSize(60, 60);

    btnLayout4->addWidget(m_inventoryCheckBtn);
    btnLayout4->addWidget(m_inventoryRecordBtn);

    QLabel *label4 = new QLabel("盘点", area4);
    label4->setFixedHeight(20);
    label4->setAlignment(Qt::AlignCenter);
    label4->setStyleSheet("background-color: #87CEFA; color: #333333; font-weight: bold; border-radius: 2px;");

    layout4->addLayout(btnLayout4);
    layout4->addWidget(label4);
    mainMenuLayout->addWidget(area4);

    // 功能区5：其他
    QWidget *area5 = new QWidget(m_topMenu);
    area5->setFixedHeight(100);
    area5->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout5 = new QVBoxLayout(area5);
    layout5->setContentsMargins(5, 5, 5, 5);
    layout5->setSpacing(5);

    QHBoxLayout *btnLayout5 = new QHBoxLayout();
    btnLayout5->setContentsMargins(0, 0, 0, 0);
    btnLayout5->setSpacing(10);

    m_helpBtn = new QPushButton("帮助", area5);
    m_helpBtn->setFixedSize(60, 60);
    m_exitBtn = new QPushButton("退出", area5);
    m_exitBtn->setFixedSize(60, 60);

    btnLayout5->addWidget(m_helpBtn);
    btnLayout5->addWidget(m_exitBtn);

    QLabel *label5 = new QLabel("其他", area5);
    label5->setFixedHeight(20);
    label5->setAlignment(Qt::AlignCenter);
    label5->setStyleSheet("background-color: #DDA0DD; color: #333333; font-weight: bold; border-radius: 2px;");

    layout5->addLayout(btnLayout5);
    layout5->addWidget(label5);
    mainMenuLayout->addWidget(area5);
    mainMenuLayout->addStretch();

    // 设置按钮样式
    QString buttonStyle =
        "QPushButton {"
        "   background-color: #FFFFFF;"
        "   color: #333333;"
        "   border: 1px solid #CCCCCC;"
        "   border-radius: 3px;"
        "   font-size: 12px;"
        "   font-family: Microsoft YaHei;"
        "}"
        "QPushButton:hover {"
        "   background-color: #F5F5F5;"
        "   border-color: #BBBBBB;"
        "}"
        "QPushButton:pressed {"
        "   background-color: #E8E8E8;"
        "   border-color: #AAAAAA;"
        "}";

    QList<QPushButton*> buttons = m_topMenu->findChildren<QPushButton*>();
    for (QPushButton *btn : qAsConst(buttons)) {
        btn->setStyleSheet(buttonStyle);
    }

    // 连接按钮与页面切换
    connect(m_userManageBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(0);
    });
    connect(m_roleManageBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(1);
    });
    connect(m_permissionManageBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(2);
    });
    connect(m_dataBackupBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(3);
    });
    connect(m_dataRestoreBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(4);
    });
    connect(m_systemConfigBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(5);
    });
    connect(m_logViewBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(6);
    });
    connect(m_aboutBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(7);
    });
    connect(m_shiftChangeBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(8);
    });
    connect(m_shiftRecordBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(9);
    });
    connect(m_inventoryCheckBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(10);
    });
    connect(m_inventoryRecordBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(11);
    });
    connect(m_helpBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(12);
    });
    connect(m_exitBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(13);
    });
}
